package Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:hfj
 * 功能:257.二叉树的所有路径
 * 日期: 2025/11/11 16:30
 */
public class BinaryTreePaths {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        solve(root, "", list);
        return list;
    }
    public void solve(TreeNode root, String cur, List<String> list){
        if(root == null){
            return;
        }
        cur += root.val;//加入当前节点
        //利用前序遍历
        if(root.left == null && root.right == null){
            list.add(cur);//处理根节点
        }else{
            solve(root.left, cur+"->", list);
            solve(root.right, cur+"->", list);
        }
    }
}
